package i_iterator.a_design_pattern;

/**
 * Created by wug on 2016/7/13 0013.
 */
public class MyMap<T> implements MyCollection {
    private String[] keys = new String[10];
    private Object[] values = new Object[10];
    private int index = 0;

    public void put(String key, T value) {
        if (index >= keys.length) {
            String[] keyTemp = keys;
            Object[] valueTemp = values;
            keys = new String[keys.length + 10];
            values = new Object[values.length + 10];
            for (int i = 0; i < keys.length; i++) {
                keys[i] = keyTemp[i];
                values[i] = valueTemp[i];
            }
        }
        keys[index] = key;
        values[index] = value;
        index++;
    }

    public T get(String key) {
        for (int i = 0; i < keys.length; i++) {
            if (keys[i].equals(key)) {
                return (T)values[i];
            }
        }
        return null;
    }

    @Override
    public MyIterator iterator() {
        return new MyMapIterator<>(keys, values);
    }
}
